import axios from "axios";
import QS from "qs";
// import store from "../store"; // 引入qs模块，用来序列化post类型的数据

//定义异常返回信息
const error = {
    code: 0,
    msg: "请求异常",
    data: {}
};

// 创建axios实例
const service = axios.create({
    baseURL: '',    // api的baseURL
    timeout: 20000, // 请求超时时间
    params: {
        // 请求参数
    }
});

// request拦截器
service.interceptors.request.use(config => {
    // config 添加 token 值
    // config.headers['token'] = getToken();  // getToken()是我的获取值方法，系统校验使用
    return config
})
export default service
//前置拦截，添加请求头
axios.interceptors.request.use(config =>{
    // store.commit('showLoading',true);
    return config
},error => {
    if(error && error.response)
    {
        let statusCode = error.response.status;
        console.log(statusCode);
        switch (statusCode)
        {
            case 500:
                this.$message.error("服务器内部错误，请联系管理员");
                break;
            case 400:
                this.$message.error("参数输入有误，请检查输入参数是否正确");
                break;
            case 401:
                this.$message.error("该用户并未授权，请重新登录");
                break;
            case 404:
                this.$message.error("网络异常");
                break;
            case 0:
                this.$message.error("网络异常");
                break;
        }
    }
    console.log(error);
    console.log(1);
    // store.commit('showLoading',false)
    return Promise.reject(error);
})

axios.interceptors.response.use(config=>{
    // store.commit('showLoading',false)
    return config
},error => {
    if(error && error.response)
    {
        let statusCode = error.response.status;
        switch (statusCode)
        {
            case 500:
                this.$message.error("服务器内部错误，请联系管理员");
                break;
            case 400:
                this.$message.error("参数输入有误，请检查输入参数是否正确");
                break;
            case 401:
                this.$message.error("该用户并未授权，请重新登录");
                break;
            case 404:
                this.$message.error("网络异常");
                break;
            case 0:
                this.$message.error("网络异常");
                break;
        }
    }
    // store.commit('showLoading',false)
    return Promise.reject(error);
})

export async function request(url, data, type) {
    try {
        // url="http://127.0.0.1:4523/mock/483541" +url; // create
        url = "http://125.221.232.243:8095" + url;
        // url = "http://192.168.177.130:8082" + url;
        // url = "http://47.107.239.69:8094" + url;
        //  url = "http://localhost:8083" + url;
        // url = "http://127.0.0.1:4523/mock/483541" + url // del

        // 判断请求类型，部署到对方公司云平台时，使用下面这个获取token
        if (type === "get") {
            console.log('(await axios.get(url, { params: data })).data')
            console.log((await axios.get(url, { params: data })).data)
            return (await axios.get(url, { params: data })).data;
        } else if (type === "post") {
            console.log(QS.stringify(data));
            return (
                await axios.post(url, data, {
                    headers: {"token":
                            localStorage.getItem("token") },})
            ).data;
        }else if (type === "post1") {
            console.log(QS.stringify(data));
            return (
                await axios.post(url, data, {
                    headers: {"token":
                            localStorage.getItem("token") },})
            );
        } else if(type === "get1") {
            return (await axios.get(url, {headers: {
                    "token":localStorage.getItem("token")
                }}))   }
        else if(type === "put") {
            return (await axios.put(url, {headers: {
                    "token":localStorage.getItem("token")
                }}))   }
        else if(type === "delete") {
            return (await axios.delete(url, {headers: {
                    "token":localStorage.getItem("token")
                }}))   }
        else {
            return error;
        }
    } catch (e) {
        console.log(e);
        return error;
    }
}
//    // 本地部署测试时使用这个，token已经是写好了的
//    if (type === "get") {
//      return (await axios.get(url, { params: data ,headers: {
//          "token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"
//        }})).data;
//    } else if (type === "post") {
//      console.log(QS.stringify(data));
//      return (
//          await axios.post(url, data, {
//            headers: {"Content-Type":
//                  "multipart/form-data","token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw" },})
//      ).data;
//    }else if (type === "post1") {
//      console.log(QS.stringify(data));
//      return (
//          await axios.post(url, data, {
//            headers: {"token": "eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"}})
//      );
//    } else if(type === "get1") {
//      return (await axios.get(url, {headers: {
//          "token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"
//        }}))   }
//    else if(type === "put") {
//      return (await axios.put(url, {headers: {
//          "token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"
//        }}))   }
//    else if(type === "delete") {
//      return (await axios.delete(url, {headers: {
//          "token":"eyJUeXBlIjoiSnd0IiwidHlwIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ0b2tlbiI6IntcImNoaW5lc2VOYW1lXCI6XCLnrqHnkIblkZhcIixcImNsaWVudElkXCI6XCJjb21cIixcImlkXCI6MSxcImp0aVwiOlwiMzU1MTdhYmQtYjBiZi00OGQ3LTllNmMtMTYwMDI2Y2FkYmRiXCIsXCJtZW1iZXJOYW1lXCI6XCJhZG1pblwiLFwibW9iaWxlXCI6XCIxMzkyMDk5MTcxMVwifSJ9.CGg1jux6iAuQRXXmBa5NTjJCuCzqL3kGZGXpZI4whIw"
//        }}))   }
//    else {
//      return error;
//    }
//  } catch (e) {
//    console.log(e);
//    return error;
//  }
// }
